package com.example.java_chatroom.config;

import com.example.java_chatroom.controller.TestWebSocketController;
import com.example.java_chatroom.controller.WebSocketController;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Autowired
    TestWebSocketController testWebSocketController;

    @Autowired
    WebSocketController webSocketController;

    //通过这个将我们实现的handler类给注册到指定的路径上
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        //当websocket的请求路径是test的时候，就会调用testWebSocketController里面的方法
        //registry.addHandler(testWebSocketController, "/test");

        registry.addHandler(webSocketController, "/WebSocketMessage")
                //添加拦截器，使http中的session内容转移到websocketsession中,实现在websocket中获取httpsession中的内容
                .addInterceptors(new HttpSessionHandshakeInterceptor());
    }
}
